<?php
  2 /*
  3     方倍工作室
  4     CopyRight 2013 All Rights Reserved
  5 */
  6 
  7 define("TOKEN", "weixin");
  8 
  9 $wechatObj = new wechatCallbackapiTest();
 10 if (!isset($_GET['echostr'])) {
 11     $wechatObj->responseMsg();
 12 }else{
 13     $wechatObj->valid();
 14 }
 15 
 16 class wechatCallbackapiTest
 17 {
 18     public function valid()
 19     {
 20         $echoStr = $_GET["echostr"];
 21         if($this->checkSignature()){
 22             echo $echoStr;
 23             exit;
 24         }
 25     }
 26 
 27     private function checkSignature()
 28     {
 29         $signature = $_GET["signature"];
 30         $timestamp = $_GET["timestamp"];
 31         $nonce = $_GET["nonce"];
 32         $token = TOKEN;
 33         $tmpArr = array($token, $timestamp, $nonce);
 34         sort($tmpArr);
 35         $tmpStr = implode($tmpArr);
 36         $tmpStr = sha1($tmpStr);
 37 
 38         if($tmpStr == $signature){
 39             return true;
 40         }else{
 41             return false;
 42         }
 43     }
 44 
 45     public function responseMsg()
 46     {
 47         $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
 48         if (!empty($postStr)){
 49             $this->logger("R ".$postStr);
 50             $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
 51             $RX_TYPE = trim($postObj->MsgType);
 52 
 53             switch ($RX_TYPE)
 54             {
 55                 case "event":
 56                     $result = $this->receiveEvent($postObj);
 57                     break;
 58                 case "text":
 59                     $result = $this->receiveText($postObj);
 60                     break;
 61                 case "location":
 62                     $result = $this->receiveLocation($postObj);
 63                     break;
 64                 case "voice":
 65                     $result = $this->receiveVoice($postObj);
 66                     break;
 67                 default:
 68                     $result = "";
 69                     break;
 70             }
 71             $this->logger("T ".$result);
 72             echo $result;
 73         }else {
 74             echo "";
 75             exit;
 76         }
 77     }
 78     
 79     private function receiveEvent($object)
 80     {
 81         $content = "";
 82         switch ($object->Event)
 83         {
 84             case "subscribe":
 85                 $content = "欢迎关注方倍工作室\n".
 86                 "天气：请发送位置\n".
 87                 "笑话：请对我说话\n".
 88                 "点歌：请发送 歌手名@歌曲名\n";
 89                 break;
 90             case "unsubscribe":
 91                 $content = "取消关注";
 92                 break;
 93             default:
 94                 $content = "";
 95                 break;
 96         }
 97         $result = $this->transmitText($object, $content);
 98         return $result;
 99     }
100   
101     private function receiveText($object)
102     {
103         $keyword = trim($object->Content);
104         if (strpos($keyword, "@")){
105             $music = explode("@",$keyword);
106             $url = "http://api100.duapp.com/music/?appkey=".$object->ToUserName."&singer=".urlencode($music[0])."&song=".urlencode($music[1]);
107             $output = file_get_contents($url);
108             $content = json_decode($output, true);
109             $result = $this->transmitMusic($object, $content);
110         }else{
111             $content = date("Y-m-d H:i:s",time());;
112             $result = $this->transmitText($object, $content);
113         }
114         return $result;
115     }
116 
117     private function receiveLocation($object)
118     {
119         $url = "http://api100.duapp.com/weather2/?appkey=".$object->ToUserName."&lat=".$object->Location_X."&lng=".$object->Location_Y;
120         $output = file_get_contents($url);
121         $content = json_decode($output, true);
122         $result = $this->transmitNews($object, $content);
123         return $result;
124     }
125 
126     private function receiveVoice($object)
127     {
128         $url = "http://api100.duapp.com/joke/?appkey=".$object->ToUserName;
129         $output = file_get_contents($url);
130         $content = json_decode($output, true);
131         $result = $this->transmitText($object, $content);
132         return $result;
133     }
134 
135     private function transmitText($object, $content)
136     {
137         $textTpl = "<xml>
138 <ToUserName><![CDATA[%s]]></ToUserName>
139 <FromUserName><![CDATA[%s]]></FromUserName>
140 <CreateTime>%s</CreateTime>
141 <MsgType><![CDATA[text]]></MsgType>
142 <Content><![CDATA[%s]]></Content>
143 </xml>";
144         $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
145         return $result;
146     }
147 
148     private function transmitNews($object, $arr_item)
149     {
150         if(!is_array($arr_item))
151             return;
152 
153         $itemTpl = "    <item>
154         <Title><![CDATA[%s]]></Title>
155         <Description><![CDATA[%s]]></Description>
156         <PicUrl><![CDATA[%s]]></PicUrl>
157         <Url><![CDATA[%s]]></Url>
158     </item>
159 ";
160         $item_str = "";
161         foreach ($arr_item as $item)
162             $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
163 
164         $newsTpl = "<xml>
165 <ToUserName><![CDATA[%s]]></ToUserName>
166 <FromUserName><![CDATA[%s]]></FromUserName>
167 <CreateTime>%s</CreateTime>
168 <MsgType><![CDATA[news]]></MsgType>
169 <Content><![CDATA[]]></Content>
170 <ArticleCount>%s</ArticleCount>
171 <Articles>
172 $item_str</Articles>
173 </xml>";
174 
175         $result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($arr_item));
176         return $result;
177     }
178 
179     private function transmitMusic($object, $musicArray)
180     {
181         $itemTpl = "<Music>
182     <Title><![CDATA[%s]]></Title>
183     <Description><![CDATA[%s]]></Description>
184     <MusicUrl><![CDATA[%s]]></MusicUrl>
185     <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
186 </Music>";
187 
188         $item_str = sprintf($itemTpl, $musicArray['Title'], $musicArray['Description'], $musicArray['MusicUrl'], $musicArray['HQMusicUrl']);
189 
190         $textTpl = "<xml>
191 <ToUserName><![CDATA[%s]]></ToUserName>
192 <FromUserName><![CDATA[%s]]></FromUserName>
193 <CreateTime>%s</CreateTime>
194 <MsgType><![CDATA[music]]></MsgType>
195 $item_str
196 </xml>";
197 
198         $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
199         return $result;
200     }
201     
202     private function logger($log_content)
203     {
204     }
205 }
206 ?>